package com.sgb.kjwl.utils;

import android.util.Log;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextSizeUtils {

    public static String singleTextLine(int textViewWidth,float textSize,String s){
//        int sum = getChineseCount(s) + (s.length() - getChineseCount(s))/2;
        if (s.length() * textSize > textViewWidth) {
            int num = getNum(textViewWidth,textSize,0);
            num = num - 1;
            return s.substring((s.length()-num)/2,(s.length()-num)/2+num);
        }else {
            return s;
        }
    }

    private static int getNum(int textViewWidth,float textSize,int num){
        if (num * textSize >= textViewWidth){
            return num;
        }else {
            return getNum(textViewWidth,textSize,num+1);
        }
    }

    public static int getChineseCount(String content) {
        int count = 0;
        String regEx = "[\u4e00-\u9fa5]";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(content);
        while(matcher.find()) {
            count ++;
        }
        return count;
    }
}
